﻿using UnityEngine;
using UnityEditor;
using System.IO;
using Config.Internal;
using System;

namespace Config.Edit
{
    [Obsolete("Use ConfigEditor instead")]
    public class SpawnConfigScriptWindow : EditorWindow
    {
        // [MenuItem("Config/Create Config Script")]
        static void CreatConfigScript()
        {
            EditorWindow.GetWindow(typeof(SpawnConfigScriptWindow));
        }

        string sptName = string.Empty;
        const string PathFmt = "{0}/{1}.cs";

        void OnGUI()
        {
            EditorGUILayout.BeginHorizontal();
            sptName = EditorGUILayout.TextField("Script Name", sptName);
            if (GUILayout.Button("Create"))
            {
                try
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.AppendFormat(content, sptName, "{", "}");

                    if (!System.IO.Directory.Exists(ConfigConst.ConfigScriptPath))
                        System.IO.Directory.CreateDirectory(ConfigConst.ConfigScriptPath);

                    File.WriteAllText(
                        string.Format(PathFmt, ConfigConst.ConfigScriptPath, sptName),
                        sb.ToString(), System.Text.Encoding.UTF8);
                }
                catch (System.Exception e)
                {
                    Debug.LogError(e.Message);
                }
                AssetDatabase.Refresh();
            }
            EditorGUILayout.EndHorizontal();
        }

        string content =
    @"using System;
using Config.Internal;

namespace Config
{1}
    [Serializable]
    [ConfigData]
    public class {0} : ConfigDataBase
    {1}
        public override uint Key()
        {1}
            return ID;
        {2}

        public uint ID;

        public override void DeserializeMember()
        {1}
        
        {2}
    {2}

    [Serializable]
    public class {0}Config : ConfigBase<{0}>
    {1}
        
    {2}
{2}";
    }

}

